def generate_insert_statement(table_a, table_b, columns_a, primary_keys_a, columns_b, primary_keys_b, filter_condition=None, dblink=None):
    """
    生成 SQL INSERT 语句
    
    参数:
        table_a: 目标表名
        table_b: 源表名
        columns_a: 目标表的列名列表
        primary_keys_a: 目标表的主键列名列表
        columns_b: 源表的列名列表
        primary_keys_b: 源表的主键列名列表
        filter_condition: 筛选条件，用于过滤表B的数据（可选）
        dblink: 数据库链接信息，用于表B是远程表的情况（可选）
    
    返回:
        字典，包含状态和结果信息
    """
    # 参数验证
    if not all([table_a, table_b, columns_a, columns_b]):
        return {"success": False, "error": "表名和列名列表不能为空", "statement": None}
    
    # 处理主键为空的情况
    if not primary_keys_a and not primary_keys_b:
        # 如果两个表的主键都为空，则认为所有列都是主键
        primary_keys_a = columns_a
        primary_keys_b = columns_b
    elif not primary_keys_a or not primary_keys_b:
        return {"success": False, "error": "一个表有主键而另一个表没有", "statement": None}
    
    # 验证主键数量和名称是否一致
    if len(primary_keys_a) != len(primary_keys_b):
        return {"success": False, "error": "两个表的主键数量不一致", "statement": None}
    
    # 检查主键兼容性：确保A的主键都是B的主键
    for pk_a in primary_keys_a:
        if pk_a not in columns_b:
            return {"success": False, "error": f"目标表的主键 '{pk_a}' 不在源表中", "statement": None}
    
    # 构建列映射关系
    column_mapping = {}
    for col_a in columns_a:
        if col_a in columns_b:
            column_mapping[col_a] = col_a
        else:
            return {"success": False, "error": f"目标表的列 '{col_a}' 不在源表中", "statement": None}
    
    # 验证主键映射
    for pk_a, pk_b in zip(primary_keys_a, primary_keys_b):
        if column_mapping.get(pk_a) != pk_b:
            return {"success": False, "error": f"主键映射不匹配 - 目标表 '{pk_a}' 映射到源表 '{pk_b}'", "statement": None}
    
    # 构建 INSERT 语句
    insert_parts = []
    
    # INSERT 头部，明确列出目标表列名
    columns_str = "(" + ", ".join(columns_a) + ")" if columns_a != columns_b else '' 
    insert_parts.append(f"INSERT INTO {table_a} {columns_str}")

    # VALUES 部分
    insert_parts.append("SELECT")
    
    # 源表列名列表，若两表列相同则用 *
    if columns_a == columns_b:
        insert_parts.append("  *")
    else:
        source_columns = [column_mapping[col_a] for col_a in columns_a]
        source_columns_str = ", ".join(source_columns)
        insert_parts.append(f"{source_columns_str}")
    
    # FROM 部分，处理 DBlink
    if dblink:
        # 使用 DBlink 格式的表名
        full_table_b = f"{table_b}@{dblink}"
        insert_parts.append(f"FROM {full_table_b}")
    else:
        # 常规表名处理
        insert_parts.append(f"FROM {table_b}")
    
    # WHERE 部分，处理筛选条件
    if filter_condition:
        insert_parts.append(f"WHERE {filter_condition}")
    
    # 添加分号结束语句
    insert_parts.append(";")
    
    # 组合完整语句
    statement = " ".join(insert_parts)
    
    # 返回成功结果
    return {"success": True, "error": None, "statement": statement}


# 示例用法
if __name__ == "__main__":
    # 示例 1: 标准情况
    print('示例 1: 标准情况')
    result = generate_insert_statement(
        table_a="users",
        table_b="temp_users",
        columns_a=["id", "name", "email", "created_at"],
        primary_keys_a=["id"],
        columns_b=["id", "name", "email", "created_at"],
        primary_keys_b=["id"]
    )
    print(result['statement'] + "\n" if result['success'] else result['error'] + "\n")
    
    # 示例 2: A的列是B的列的子集
    print('示例 2: A的列是B的列的子集')
    result = generate_insert_statement(
        table_a="users",
        table_b="temp_users",
        columns_a=["id", "name", "email"],  # A表只有3列
        primary_keys_a=["id"],
        columns_b=["id", "name", "email", "status", "updated_at"],  # B表有5列
        primary_keys_b=["id"]
    )
    print(result['statement'] + "\n" if result['success'] else result['error'] + "\n")
    
    # 示例 3: 添加筛选条件
    print('示例 3: 列相同，添加筛选条件')
    result = generate_insert_statement(
        table_a="users",
        table_b="temp_users",
        columns_a=["id", "name", "email", "status", "updated_at"],
        primary_keys_a=["id"],
        columns_b=["id", "name", "email", "status", "updated_at"],
        primary_keys_b=["id"],
        filter_condition="status = 'active' AND updated_at > '2023-01-01'"
    )
    print(result['statement'] + "\n" if result['success'] else result['error'] + "\n")
    
    # 示例 4: 表B是通过DBlink访问的远程表
    print('示例 4: 表B是通过DBlink访问的远程表')
    result = generate_insert_statement(
        table_a="local_users",
        table_b="remote_users",
        columns_a=["id", "name", "email"],
        primary_keys_a=["id"],
        columns_b=["id", "name", "email", "status"],
        primary_keys_b=["id"],
        dblink="remote_db_link"
    )
    print(result['statement'] + "\n" if result['success'] else result['error'] + "\n")
    
    # 示例 5: 表B是通过DBlink访问的远程表，且有筛选条件
    print('示例 5: 表B是通过DBlink访问的远程表，且有筛选条件')
    result = generate_insert_statement(
        table_a="local_products",
        table_b="remote_products",
        columns_a=["sku", "name", "price"],
        primary_keys_a=["sku"],
        columns_b=["sku", "name", "price", "stock", "last_updated"],
        primary_keys_b=["sku"],
        filter_condition="stock > 0 AND last_updated > SYSDATE - 7",
        dblink="remote_db_link"
    )
    print(result['statement'] + "\n" if result['success'] else result['error'] + "\n")